<template>
  <div class="home-wrap">
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="slide in slides" :key="slide.url" v-bind:style="{backgroundImage: 'url('+slide.url+')'}"></div>
      </div>
      <!-- Add Pagination -->
      <div class="swiper-pagination" v-bind:style="{width: 'calc(('+ slides.length +' * 0.4rem', left: 'calc(((100% - '+ slides.length +' * 0.4rem) / 2'}"></div>
    </div>
    <div class="home-title">最新列表</div>
    <div class="home-slides">
      <div class="home-slide" v-for="list in lists" :key="list.name">
        {{list.name}}
      </div>
    </div>
  </div>
</template>

<script>
  import {getCookie} from "../../assets/js/cookie";
  import Swiper from "swiper";
  import "swiper/dist/css/swiper.min.css";
  let mySwiper;

  export default {
    name: "home",
    data() {
      return {
        slides: [
          {'url': '../../static/img/banner01.jpg'},
          {'url': '../../static/img/banner02.jpg'},
          {'url': '../../static/img/banner03.jpg'},
          {'url': '../../static/img/banner04.jpg'}
        ],
        lists: ''
      }
    },
    mounted() {
      // 页面挂载获取保存的cookie值，渲染到页面上
      let uname = getCookie('username');
      console.log(uname);
      // 如果cookie不存在，则跳转到登录页
      if (uname == '') {
        this.$router.push('/');
      }
      this.lunbo();
    },
    methods: {
      lunbo() {
        mySwiper = new Swiper('.swiper-container', {
          pagination: '.swiper-pagination',
          paginationClickable: true,
          autoplay: 2500,
          loop: true,
          autoplayDisableOnInteraction : false
        })
      }
    }
  }
</script>

<style lang="less">
  .home-wrap {
    width: 100%;
    height: auto;
    margin: 1px auto 0;
    overflow: hidden;
    .swiper-container {
      width: 100%;
      height: 4rem;
      .swiper-slide {
        width: 100%;
        height: 100%;
        background-size: 100% 100%;
      }
      .swiper-pagination {
        padding: 0.1rem 0;
        bottom: 0;
        .swiper-pagination-bullet {
          width: 0.2rem;
          height: 0.2rem;
          background-color: #ffffff;
          border: none;
          margin: 0 0.1rem;
          opacity: 0.8;
          display: block;
          float: left;
          &.swiper-pagination-bullet-active {
            background-color: red;
          }
        }
      }
    }
    .home-title {
      width: 6.9rem;
      height: 0.6rem;
      border-bottom: 1px solid #999999;
      margin: 0.3rem auto 0.2rem;
      font-size: 0.3rem;
      color: #333333;
      line-height: 0.6rem;
      text-align: left;
    }
    .home-lists {
      width: 6.9rem;
      height: auto;
      margin: 0 auto;
      display: flex;
      flex-wrap: wrap;
      align-content: space-between;
      justify-content: space-between;
      .home-list {
        width: 3.4rem;
        height: 2rem;
        background-color: red;
        margin-bottom: 0.1rem;
      }
    }
  }
</style>
